var vue = new Vue({
  el: '#record',
  data: {
    multipleSelection: [],
    title: '',
    days: '',
    record: '',
    meetingData: [{ id: 1 }, { id: 2 }, { id: 3 }],
    totalSize: 0,
    pageIndex: 1,
    pageSize: 10,
    directorOptions: [{
      label: '主任1',
      value: '1'
    }, {
      label: '主任2',
      value: '2'
    }],
    companyOptions: [{
      label: '辽宁1',
      value: '1'
    }, {
      label: '辽宁2',
      value: '2'
    }, {
      label: '辽宁3',
      value: '3'
    }, {
      label: '辽宁4',
      value: '4'
    }, {
      label: '辽宁5',
      value: '5'
    }, {
      label: '辽宁6',
      value: '6'
    }, {
      label: '辽宁7',
      value: '7'
    }],
    pickerOptions1: {
      disabledDate(time) {
        return time.getTime() > Date.now()
      }
    },
    // 添加界面表单
    form: {
      director: '',
      date_meeting: '',
      company: [],
      theme: ''
    },
    //表单验证规则
    rules: {
      director: [{
        required: true,
        message: '请选择主任人',
        trigger: 'blur'
      }],
      date_meeting: [{
        type: 'date',
        required: true,
        message: '请选择时间',
        trigger: 'blur'
      }],
      company: [{
        type: 'array',
        required: true,
        message: '选择参会单位',
        trigger: 'change'
      }],
      theme: [{
        required: true,
        message: '请输入会议主题',
        trigger: 'blur',
        maxlength: 1000
      }]
    },

    formLabelWidth: '120px',
    //对话框显示状态
    addVisible: false

  },
  created: function() {
    this.getDic()
  },
  mounted() {
    this.initGrid()

  },

  methods: {
    // 添加记录
    add: function(id) {
      this.addVisible = true
    },

    // 删除
    deleteMeeting: function(rid) {
      if (this.multipleSelection.length === 0) {
        this.$message({
          showClose: true,
          message: '请至少选择一条数据',
          type: 'warning'
        })
        return false
      }
      var ids = this.multipleSelection.map(item => item.id).join()
      if (confirm('确定删除吗')) {
        var that = this
        $.post('weekMeeting/removeForBatch', {
          ids: ids
        }, function(result) {
          if (result.data) {
            that.$message({
              message: '删除成功',
              type: 'success'
            })
            that.initGrid()
          } else {
            that.$message({
              message: '删除失败',
              type: 'warning'
            })
          }
        })
      }
    },
    // 获取字典下拉菜单
    getDic: function() {
      var that = this
      $.get('weekMeeting/getDic', function(result) {
        that.directorOptions = result.data.user
        that.companyOptions = result.data.org
      })
    },
    // 初始化表格
    initGrid: function() {
      var that = this
      $.post('weekMeeting/listByPageByDate', {
        index: that.pageIndex,
        size: that.pageSize
      }, function(result) {
        console.log(result)
        that.totalSize = result.data.total
        that.meetingData = result.data.data
      })
    },
    //翻页
    handleSizeChange(val) {
      this.pageSize = val
      this.initGrid()
    },
    //改变每页显示条数
    handleCurrentChange(val) {
      this.pageIndex = val
      this.initGrid()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    //添加会议
    addMeeting: function() {
      var that = this
      var date_meetingStr = that.form.date_meeting.format('yyyy-MM-dd')
      var id_company_join = this.form.company.map(item => item.value).join(',')
      var company_join = this.form.company.map(item => item.label).join(',')
      $.post('weekMeeting/save', {
        director: that.form.director,
        date_meeting: date_meetingStr,
        id_company_join: id_company_join,
        company_join: company_join,
        theme: that.form.theme
      }, function(result) {
        if (result.data) {
          that.$message({
            message: '增加成功',
            type: 'success'
          })
          that.addVisible = false
          that.initGrid()
        } else {
          that.$message({
            message: '增加失败',
            type: 'warning'
          })
        }
      })
    },
    //关闭dialog前执行的方法
    handleClose(done) {
      var that = this
      this.$confirm('确认关闭？')
        .then(_ => {
          if (that.addVisible) {
            that.addVisible = false
            this.$refs['form'].resetFields()
          }
        })
        .catch(_ => {
        })
    },
    //添加表单验证
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addMeeting()

        } else {

          return false
        }
      })
    }

  }
})
